package com.zhuimeng.cusumer;

import com.zhuimeng.cusumer.bean.ConsumerComponent;
import org.apache.dubbo.config.*;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class CosumerApplication {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DubboConfiguration.class);
        ConsumerComponent service = context.getBean(ConsumerComponent.class);

        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(3, 10,
                100, TimeUnit.MICROSECONDS, new LinkedBlockingQueue<Runnable>());
        while (true){
            threadPool.submit(() -> service.method1());
            threadPool.submit(() -> service.method2());
            threadPool.submit(() -> service.method3());
        }
    }

    @Configuration
    @ComponentScan("com.zhuimeng.cusumer.bean")
    @EnableDubbo
    static class DubboConfiguration {
        @Bean //服务提供者信息配置
        public ProviderConfig providerConfig() {
            ProviderConfig providerConfig = new ProviderConfig();
            providerConfig.setTimeout(100000);
            return providerConfig;
        }

        @Bean
        public ConsumerConfig consumerConfig(){
            ConsumerConfig consumerConfig = new ConsumerConfig();
            consumerConfig.setTimeout(100000);
            return consumerConfig;
        }

        @Bean //应用信息配置
        public ApplicationConfig applicationConfig() {
            ApplicationConfig applicationConfig = new ApplicationConfig();
            applicationConfig.setName("dubbo-annotation-consumer");
            return applicationConfig;
        }

        @Bean //注册中心信息配置
        public RegistryConfig registryConfi() {
            RegistryConfig registryConfig = new RegistryConfig();
            registryConfig.setAddress("zookeeper://127.0.0.1:2181?timeout=1000000");
            registryConfig.setProtocol("zookeeper");
            return registryConfig;
        }

        @Bean //协议配置
        public ProtocolConfig protocolConfig() {
            ProtocolConfig protocolConfig = new ProtocolConfig();
            protocolConfig.setName("dubbo");
            protocolConfig.setPort(20880);
            return protocolConfig;
        }
    }
}
